// 用电单车产量温度卡
var numberFormate = iot.getNumberFormat("0.00");
var timeFormatMap = {
    'year': "%Y",
    'month': "%Y-%m",
    'day': "%Y-%m-%d"
};
dev.action = function(resp,param){
    //获取工厂名称
    var factoryName = getFactoryName(param.workSpaceName);
    var theTime = param.time;
    var lastTime = iot.getTimeByTime(theTime);
    var timeTypeByTime = getTimeType(theTime);
    var workSpace = param.workSpace;
    var jsData =  iot.list();
    var db = iot.db();

    var totalSumResult;
    var totalSum = 0;
    var lastTotalSum = 0.00;

    totalSumResult = getTotalSum(db, timeTypeByTime, workSpace, theTime, lastTime, 1);

    totalSumResult.forAll(function(item){
        if (item.time === theTime) {
            totalSum = item.totalSum;
        }
        if (item.time === lastTime) {
            lastTotalSum = item.totalSum;
        }
    });

    var yieldId = getFactoryYieldId(factoryName)

    var yieldResult = getFactoryYield(yieldId, theTime)

    var totalUseNum = 0;
    var lastTimeTotalSum = 0;
    var yieldUseNum = 0;
    var bicycleUseNum = 0;

    if (totalSum) {
        totalUseNum = numberFormate.format(totalSum);
    }
    if (lastTotalSum) {
        lastTimeTotalSum = numberFormate.format(lastTotalSum);
    }
    if (yieldResult) {
        yieldUseNum = yieldResult.totalSum;
    }
    if(yieldUseNum && yieldUseNum !== 0){
        bicycleUseNum = numberFormate.format(totalUseNum/yieldUseNum);
    }

    //start KPI
    var sn = "H001";
    //获取目标id
    var targetValueIdResult = db.qry("select es2.id from energy_struc es1\n" +
        "JOIN energy_struc es2\n" +
        "on es1.id = es2.pid\n" +
        "where es1.id =" + workSpace);
    var targetValueId = '';
    targetValueIdResult.forAll(function(item){
        targetValueId = targetValueId + item.id + ","
    });
    targetValueId = targetValueId + 0;

    if(targetValueId == "0"){
        targetValueId = workSpace;
    }
    var targetValueTime = theTime;
    if(theTime && theTime.length() == 10){
        targetValueTime = theTime.substring(0,7);
    }
    //获取目标值
    var targetValueResult = db.qry("select esp.name,SUM(win.dvalue) totalSum \n" +
        "from iot_dev_data_win win\n" +
        "left join energy_struc_prop esp\n" +
        "on win.dev_id = esp.dev_id\n" +
        "where esp.sn = '"+ sn +"'\n" +
        "and esp.energy_struc_id in( " +targetValueId+ " )\n" +
        "and win.the_time like '"+ targetValueTime +"%'\n" +
        "and win.data_type = esp.name");
    var targetValue = 0;
    targetValueResult.forAll(function(item){
        targetValue = item.totalSum
    });

    //计算总KPI
    //计算总KPI
    var totalKPIResult = calculateKPI(targetValue, totalUseNum);
    //计算单车KPI
    //单车KPI
    var oneTargetValue = 0;
    if(yieldUseNum && yieldUseNum !== 0){
        oneTargetValue = targetValue/yieldUseNum;
    }

    var oneKPIResult = calculateKPI(oneTargetValue, bicycleUseNum);
    //end KPI
    //start环比
    var nowTimeTotalSum = totalUseNum;

    var lastYieldResult = getFactoryYield(yieldId, lastTime)
    var lastYieldUseNum = 0;
    if (lastYieldResult) {
        lastYieldUseNum = lastYieldResult.totalSum;
    }

    //计算总环比
    var ratioResult = calculateRatio(nowTimeTotalSum, lastTimeTotalSum);

    //计算单车环比
    var oneNowTimeTotalSum = 0;
    var oneLastTimeTotalSum = 0;
    if(yieldUseNum && yieldUseNum !== 0){
        oneNowTimeTotalSum = nowTimeTotalSum/yieldUseNum;
    }
    if(lastYieldUseNum && lastYieldUseNum !== 0){
        oneLastTimeTotalSum = lastTimeTotalSum/lastYieldUseNum;
    }
    var oneRatioResult = calculateRatio(oneNowTimeTotalSum, oneLastTimeTotalSum);
    //end环比


    jsData.add({unit: "kwh", lastPeriod: ratioResult, type: "总用电", useNum: totalUseNum, kpi: totalKPIResult});
    jsData.add({unit: "kwh/辆", lastPeriod: oneRatioResult, type: "单车用电", useNum: bicycleUseNum, kpi: oneKPIResult});
    jsData.add({unit: "辆", type: "总产量", useNum: yieldUseNum});
    jsData.add({unit: "℃", type: "平均温度", useNum: 27.5});

    resp.setJsData(jsData);
}